$(document).ready(function() {
    // 初始化柱状图
    var barChart = echarts.init(document.getElementById('barChart'));
    var barOption = {
        title: {
            text: '景点投票数统计'
        },
        tooltip: {},
        legend: {
            data: ['票数']
        },
        xAxis: {
            data: barChartData.categories,
            axisLabel: {
                rotate: 30
            }
        },
        yAxis: {},
        series: [{
            name: '票数',
            type: 'bar',
            data: barChartData.data,
            itemStyle: {
                color: function(params) {
                    var colorList = ['#5470C6', '#91CC75', '#FAC858', '#EE6666', '#73C0DE', '#3BA272', '#FC8452', '#9A60B4', '#EA7CCC'];
                    return colorList[params.dataIndex % colorList.length];
                }
            }
        }]
    };
    barChart.setOption(barOption);

    // 初始化饼图
    var pieChart = echarts.init(document.getElementById('pieChart'));
    var pieOption = {
        title: {
            text: '投票比例',
            left: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: barChartData.categories
        },
        series: [
            {
                name: '投票比例',
                type: 'pie',
                radius: ['50%', '70%'],
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: '18',
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: false
                },
                data: pieChartData
            }
        ]
    };
    pieChart.setOption(pieOption);

    // 窗口大小改变时重新调整图表大小
    window.addEventListener('resize', function() {
        barChart.resize();
        pieChart.resize();
    });
});